package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.StoreBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.StoreMapper;
import com.molichuxing.services.infrastructure.dto.request.create.StoreCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.StoreModifyDto;
import com.molichuxing.services.infrastructure.dto.response.StoreCountDto;
import com.molichuxing.services.infrastructure.dto.response.StoreDto;
import com.molichuxing.services.infrastructure.service.StoreService;
import com.molichuxing.services.property.StoreIsFoursEnum;
import com.molichuxing.services.property.StoreStatusEnum;
import com.molichuxing.services.property.StoreVerifyStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 门店表 服务实现类
 * </p>
 *
 * @author zph
 * @since 2019-08-12
 */
@Service("storeService")
public class StoreServiceImpl implements StoreService {

    @Autowired
    StoreMapper storeMapper;

    /**
     * 通过ID集合获取门店信息
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Integer, StoreDto> getStoreMapByIds(List<Integer> ids) throws Exception {
        List<StoreBean> storeList = storeMapper.getBatchByIds(ids);

        Map<Integer, StoreDto> storeMap = new HashMap<Integer, StoreDto>();
        for (StoreBean store : storeList) {
            StoreDto storeBeanDto = Convert.toStoreDto(store);
            storeMap.put(storeBeanDto.getStoreId(), storeBeanDto);
        }

        return storeMap;
    }

    /**
     * 通过ID集合获取门店信息
     *
     * @param ids
     * @return
     */
    @Override
    public List<StoreDto> getStoreListByIds(List<Integer> ids) {
        List<StoreBean> storeList = storeMapper.getBatchByIds(ids);
        return Convert.toStoreDtoList(storeList);
    }
    
    /**
     * 获取门店信息
     * @param storeIdList
     * @param status
     * @param notStoreIdList
     * @return
     */
    @Override
    public List<StoreDto> getStoreListByIdsAndStatus(List<Integer> storeIdList, Integer status,List<Integer> notStoreIdList) {
    	 List<StoreBean> storeList = storeMapper.getStoreListByIdsAndStatus(storeIdList,status,notStoreIdList);
         return Convert.toStoreDtoList(storeList);
    }

    /**
     * 通过id获取门店信息
     *
     * @param id
     * @return
     */
    @Override
    public StoreDto getStoreById(Integer id) throws Exception {
        StoreBean store = storeMapper.getById(id);
        return Convert.toStoreDto(store);
    }

    /**
     * 获取门店基础信息分页列表
     *
     * @param map      key:dealerId Integer 经销商id
     *                 key:status Integer 营业状态
     *                 key:verifyStatus Integer 审核状态
     *                 key:shortName String 门店名称
     *                 key：regionId Integer 区域ID
     *                 key:startApplyTime LocalDateTime 申请开始时间
     *                 key:endApplyTime LocalDateTime 申请结束时间
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Paged<StoreDto> getStorePage(Map<String, Object> map, Integer pageNum, Integer pageSize)
            throws Exception {
        List<StoreBean> storeBeanList = storeMapper.getPage(map, pageNum, pageSize);
        long pageCount = storeMapper.getPageCount(map);
        List<StoreDto> storeDtoList = Convert.toStoreDtoList(storeBeanList);
        return new Paged<StoreDto>(storeDtoList, pageCount, pageSize);
    }

    /**
     * 通过经销商ID获取门店数量
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Integer, Integer> getStoreCountByIds(List<Integer> ids) throws Exception {
        List<StoreCountDto> storeCountList = storeMapper.getStoreCountByDealerId(ids);
        if (null == storeCountList || storeCountList.size() == 0) {
            return null;
        }

        Map<Integer, Integer> returnMap = new HashMap<>();
        for (StoreCountDto storeCountDto : storeCountList) {
            Integer dealerId = storeCountDto.getDealerId();
            Integer count = storeCountDto.getCount();
            returnMap.put(dealerId, count);
        }
        return returnMap;
    }

    /**
     * 添加门店
     *
     * @param storeCreateDto
     * @return
     */
    @Override
    public Integer createStore(StoreCreateDto storeCreateDto) throws Exception {
        Assert.notNull(storeCreateDto, "门店添加失败");
        String shortName = storeCreateDto.getShortName();
        String fullName = storeCreateDto.getFullName();
        Boolean shortNameExist = getShortNameExist(shortName);
        if (shortNameExist) {
            throw new BizException("门店名称已存在");
        }
        Boolean fullNameExist = getFullNameExist(fullName);
        if (fullNameExist) {
            throw new BizException("门店全称已存在");
        }

        StoreBean bean = Convert.toStore(storeCreateDto);
        bean.setIsShow(true);
        bean.setVerifyStatus(StoreVerifyStatusEnum.UN_VERIFY.getValue());
        storeMapper.create(bean);
        return bean.getId();
    }

    /**
     * 修改门店
     *
     * @param storeModifyDto
     * @return
     */
    @Override
    public Boolean modifyStore(StoreModifyDto storeModifyDto) throws Exception {
        Assert.notNull(storeModifyDto, "门店不存在");
        Integer storeId = storeModifyDto.getStoreId();
        Assert.notNull(storeId, "门店不存在");
        StoreBean storeBean = storeMapper.getById(storeId);
        Assert.notNull(storeBean, "门店不存在");

        String shortName = storeModifyDto.getShortName();
        String fullName = storeModifyDto.getFullName();
        String oldShortName = storeBean.getShortName();
        String oldFullName = storeBean.getFullName();
        Integer verifyStatus = storeBean.getVerifyStatus();
        if (!shortName.trim().equals(oldShortName)) {
            Boolean shortNameExist = getShortNameExist(shortName);
            if (shortNameExist) {
                throw new BizException("门店名称已存在");
            }
        }

        if (!fullName.trim().equals(oldFullName)) {
            Boolean fullNameExist = getFullNameExist(fullName);
            if (fullNameExist) {
                throw new BizException("门店全称已存在");
            }
        }

        Convert.toStore(storeModifyDto, storeBean);
        if (verifyStatus != null && StoreVerifyStatusEnum.VERIFY_REJECT.getValue() == verifyStatus) {
            storeBean.setVerifyStatus(StoreVerifyStatusEnum.UN_VERIFY.getValue());
        }
        return (storeMapper.modifyById(storeBean) > 0) ? true : false;
    }

    /**
     * 根据门店id修改门店状态
     *
     * @param storeId
     * @param status
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyStoreStatusByStoreId(Integer storeId, Integer status, Integer statusModifyType) throws Exception {
        StoreBean storeBean = storeMapper.getById(storeId);
        Assert.notNull(storeBean, "门店不存在");
        return (storeMapper.modifyStatusById(storeId, status, statusModifyType) > 0) ? true : false;
    }

    /**
     * 根据经销商修改门店状态
     *
     * @param dealerId
     * @param status
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyStoreStatusByDealerId(Integer dealerId, Integer status, Integer statusModifyType) throws Exception {
        storeMapper.modifyStatusByDealerId(dealerId, status, statusModifyType);
        return true;
    }

    /**
     * 门店审核
     *
     * @param storeId
     * @param verifyStatus
     * @param rejectReason
     * @param remark
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyVerifyStatus(Integer storeId, Integer verifyStatus,
                                      String rejectReason, String remark) throws Exception {
        storeMapper.modifyVerifyStatus(storeId, verifyStatus, rejectReason, remark);
        return true;
    }

    /**
     * 通过品牌id获取门店列表
     *
     * @param map key:brandId 品牌id
     *            key:dealerId 经销商id
     *            key:regionId 区域id
     *            key:status 营业状态
     *            key:regionId 区域ID
     *            key:statusList 营业状态集合
     * @return
     * @throws Exception
     */
    @Override
    public List<StoreDto> getStoreList(Map<String, Object> map) {
        Object status = map.get("status");
        Object statusList = map.get("statusList");
        if (null == status && null == statusList) {
            map.put("status", StoreStatusEnum.OPEN.getValue());
        }
        List<StoreBean> list = storeMapper.getList(map);
        return Convert.toStoreDtoList(list);
    }

    /**
     * 获取所有门店
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<StoreDto> getAllStoreList(Integer status) throws Exception {
        List<StoreBean> list = storeMapper.getAllList(status);
        return Convert.toStoreDtoList(list);
    }

    /**
     * 根据状态获取门店数量
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<Integer, Integer> getStoreCountByStatus(Integer dealerId) throws Exception {
        List<TabDto> statusList = storeMapper.getCountByStatus(dealerId);
        if (null == statusList || statusList.size() == 0) {
            return null;
        }

        Map<Integer, Integer> returnMap = new HashMap<>();
        for (TabDto tabDto : statusList) {
            Integer type = tabDto.getType();
            Integer count = tabDto.getCount();
            returnMap.put(type, count);
        }

        return returnMap;
    }

    /**
     * 根据审核状态获取门店数量
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<Integer, Integer> getStoreCountByVerifyStatus(Integer dealerId) throws Exception {
        List<TabDto> statusList = storeMapper.getCountByVerifyStatus(dealerId);
        if (null == statusList || statusList.size() == 0) {
            return null;
        }

        Map<Integer, Integer> returnMap = new HashMap<>();
        for (TabDto tabDto : statusList) {
            Integer type = tabDto.getType();
            Integer count = tabDto.getCount();
            returnMap.put(type, count);
        }

        return returnMap;
    }

    /**
     * 获取门店数量
     *
     * @return
     */
    @Override
    public Integer getCount(Integer dealerId) {
        return storeMapper.getCount(dealerId);
    }

    /**
     * 通过经销商id获取门店id(营业中)
     *
     * @return
     */
    @Override
    public List<Integer> getStoreIdByDealer(Integer dealerId) {
        return storeMapper.getListByDealer(dealerId, StoreStatusEnum.OPEN.getValue());
    }

    /**
     * 通过经销商id获取门店id（全部）
     *
     * @return
     */
    @Override
    public List<Integer> getAllStoreIdByDealer(Integer dealerId) {
        return storeMapper.getListByDealer(dealerId, null);
    }

    /**
     * 门店名称是否存在
     *
     * @param shortName
     * @return
     */
    @Override
    public Boolean getShortNameExist(String shortName) {
        int countByShortName = storeMapper.getCountByShortName(shortName);
        if (countByShortName > 0) {
            return true;
        }
        return false;
    }

    /**
     * 门店全称是否存在
     *
     * @param fullName
     * @return
     */
    @Override
    public Boolean getFullNameExist(String fullName) {
        int countByFullName = storeMapper.getCountByFullName(fullName);
        if (countByFullName > 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取门店信息
     *
     * @param dealerId 经销商
     * @return
     */
    @Override
    public List<StoreDto> getByDealer(Integer dealerId) {
        return Convert.toStoreDtoList(storeMapper.getByDealer(dealerId));
    }

    /**
     * 通过区域获取门店数量
     *
     * @return
     */
    @Override
    public Integer getCountByRegionId(Integer regionId, Integer status) {
        return storeMapper.getCountByRegionId(regionId, status);
    }

    /**
     * 获取门店城市
     *
     * @return
     */
    @Override
    public List<Integer> getRegionId() {
        return storeMapper.getRegionId(null,StoreStatusEnum.OPEN.getValue());
    }


    /**
     * 获取4s门店
     *
     * @param shortName
     * @return
     */
    @Override
    public List<StoreDto> getFoursStore(String shortName) {
        Map<String, Object> map = new HashMap<>();
        map.put("shortName", shortName);
        map.put("isFours", StoreIsFoursEnum.YES.getValue());
        map.put("status", StoreStatusEnum.OPEN.getValue());
        return getStoreList(map);
    }

    /**
     * 获取门店信息(营业中)
     *
     * @param dealerId 经销商
     * @return
     */
    @Override
    public List<StoreDto> getStoreByDealer(Integer dealerId) {
        Map<String, Object> map = new HashMap<>();
        map.put("status", StoreStatusEnum.OPEN.getValue());
        map.put("dealerId", dealerId);
        return getStoreList(map);
    }

    /**
     * 获取门店数量
     *
     * @param map
     * @return
     */
    @Override
    public Integer getStoreCount(Map<String, Object> map) {
        return storeMapper.getStoreCount(map);
    }

    /**
     * 获取门店集合
     *
     * @param ids
     * @return
     */
    @Override
    public List<StoreDto> getAllStoreListByIds(List<Integer> ids) {
        List<StoreBean> storeList = storeMapper.getAllBatchByIds(ids);
        return Convert.toStoreDtoList(storeList);
    }

    /**
     * 获取门店信息-根据简称模糊查询
     *
     * @param shortName 门店简称
     * @return
     */
    @Override
    public List<StoreDto> getStoreByShortNameFuzzy(String shortName) {
        return Convert.toStoreDtoList(storeMapper.getStoreByShortNameFuzzy(shortName, null, null, null));
    }

    /**
     * 根据门店名称模糊查询
     *
     * @param shortName
     * @param status
     * @return
     */
    @Override
    public List<StoreDto> getStoreByShortNameFuzzy(String shortName, Integer status, Integer isShow, List<Integer> dealerIdList) {
        return Convert.toStoreDtoList(storeMapper.getStoreByShortNameFuzzy(shortName, status, isShow, dealerIdList));
    }

    @Override
    public List<Integer> getRegionId(Integer dealerId) {
        return storeMapper.getRegionId(dealerId,null);
    }
}
